package com.hxzy.controller; /**
 * @author wzm
 * @create 2023-09-22 10:24
 * project_name:javaweb2308
 */

import com.alibaba.fastjson.JSON;
import com.hxzy.pojo.Dog;
import com.hxzy.service.DogService;
import com.hxzy.service.impl.DogServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/dog/*")
public class DogServlet extends BaseServlet {
    private DogService dogService = new DogServiceImpl();

    //新增
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("add");
        //如果是早期的jsp页面提交  两种方式都可以
        //request.getParameter("name");//可以获取 通用的
        //BufferedReader reader = request.getReader(); //post专有

        //如果是ajax提交 post请求必须使用 getReader才可以

        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");

        //获取数据
        BufferedReader reader = request.getReader();
        String s = reader.readLine();//获取了请求体中的数据  json

        //字符串转为java对象
        Dog dog = JSON.parseObject(s, Dog.class);

        //调用Service执行功能
        boolean add = dogService.add(dog);

        PrintWriter writer = response.getWriter();
        if(add){
            writer.write("success");
        }else{
            writer.write("error");
        }




    }

    //删除
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("delete");
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");


        String id = request.getParameter("id");
        Dog dog = new Dog();
        dog.setId(Integer.parseInt(id));


        boolean remove = dogService.remove(dog);

        PrintWriter writer = response.getWriter();
        if(remove){
            writer.write("success");
        }else{
            writer.write("error");
        }

    }

    //修改
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("update");
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");

        //获取数据
        BufferedReader reader = request.getReader();
        String s = reader.readLine();//获取了请求体中的数据  json

        //字符串转为java对象
        Dog dog = JSON.parseObject(s, Dog.class);

        //调用Service执行功能
        boolean update = dogService.modify(dog);

        PrintWriter writer = response.getWriter();
        if(update){
            writer.write("success");
        }else{
            writer.write("error");
        }

    }

    //查询单个
    protected void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("findById");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");

        String id = request.getParameter("id");
        Dog dog = new Dog();
        dog.setId(Integer.parseInt(id));

        Dog byId = dogService.findById(dog);//java

        //java-字符串
        String s = JSON.toJSONString(byId);
        response.getWriter().write(s);


    }

    //查询全部
    protected void findByAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("findByAll");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");

        List<Dog> byAll = dogService.findByAll();//java

        //java-字符串
        String s = JSON.toJSONString(byAll);
        response.getWriter().write(s);
    }
}
